package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
//用户信息web
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseEntity getUserInfo(Long userID, Long huanxinID, @RequestHeader("Authorization") String token){
        /*Long userId = huanxinID;
        if(null == userId){
            userId = userID;
        }
        if(null == userId){
            User user = userService.getUserByToken(token);
            if(null == user){
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error("000006","请重新登陆后再操作"));
            }
            userId = user.getId();
        }*/
        // 通过id查询用户基本信息
        UserInfoVo userInfoVo = userService.findUserInfoById(UserHolder.getUserId());
        return ResponseEntity.ok(userInfoVo);
    }


    /**
     * 保存用户基本信息
     * @param vo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfoVo vo, @RequestHeader("Authorization") String token){
        userService.updateUserInfo(vo,token);
        return ResponseEntity.ok(null);
    }


    /**
     * 更新用户头像
     */
    @RequestMapping(value = "/header",method = RequestMethod.POST)
    public ResponseEntity header(@RequestHeader("Authorization") String token, MultipartFile headPhoto) {
        userService.header(token,headPhoto);
        return ResponseEntity.ok(null);
    }
}
